package com.book.system.dao;

import com.book.system.entity.BookInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

@Slf4j
public class BookDao {

    /**
     * 初始化查询
     * @param name
     * @param classId
     * @return
     */

    public List<Map<String, Object>> getList(String name, Integer classId) {

        StringBuffer tempSql = new StringBuffer("select * from book_info where 1=1 ");

        if (name != null && !"".equals(name)) {
            tempSql.append(" and name like '%").append(name).append("%'");
        }
        if (classId != null && classId != 0) {
            tempSql.append(" and class_id= ").append(classId);
        }

        String sql = tempSql.toString();
        List<Map<String, Object>> list = BaseDao.executeQuery(sql);

        return list;
    }

    /**
     * 模糊查询
     */
    public List<Map<String, Object>> list(String name, Integer classId) {
        StringBuffer tempSql = new StringBuffer("select * from book_info where 1=1");
        if (name != null && !"".equals(name)) {
            tempSql.append(" and name like '%")
                    .append(name)
                    .append("%'");
        }

        if (classId != null && classId != 0) {
            tempSql.append(" and class_id = ")
                    .append(classId);
        }

        String sql = tempSql.toString();
        List<Map<String, Object>> list = BaseDao.executeQuery(sql);
        return list;
    }

    /**
     * 增加图书
     */
    public int add(BookInfo book){
        String sql="insert into book_info(name,author,publish,isbn,introduction,language,price,pub_date,class_id,number)"+"values(?,?,?,?,?,?,?,?,?,?)";

        return BaseDao.executeUpdate(sql,book.getName(),book.getAuthor(),book.getPublish(),book.getIsbn(),
                book.getIntroduction(),book.getLanguage(),book.getPrice(),book.getPubDate(),book.getClassId(),book.getNumber());
    }

    /**
     * 删除图书
     */
    public int delete(Integer bookId){
        String sql = "delete from book_info where book_id = ?";
        return BaseDao.executeUpdate(sql,bookId);
    }

    /**
     * 修改图书
     */
    public int updateById(BookInfo book) {

        String sql = "update book_info set name=?,author=?,publish = ?," +
                "isbn = ?,introduction = ?,language =?,price=?,pub_date=?," +
                "class_id=? ,number =? " +
                "where book_id=?";

        return BaseDao.executeUpdate(sql, book.getName(), book.getAuthor(), book.getPublish(),
                book.getIsbn(), book.getIntroduction(), book.getLanguage(), book.getPrice(),
                book.getPubDate(), book.getClassId(), book.getNumber(), book.getBookId());
    }

    /**
     * 当图书数量大于0时，减一
     *
     * @param bookId 图书编号
     * @return int
     */
    public int updateByIdSetNumber(Integer bookId) {

        //编写sql语句
        String sql = "UPDATE book_info\n" +
                "SET number = CASE \n" +
                "                 WHEN number > 0 THEN number - 1 \n" +
                "                 ELSE number \n" +
                "               END\n" +
                "WHERE book_id = ?";
        log.info("sql语句：{}", sql);
        //执行sql语句
        return BaseDao.executeUpdate(sql, bookId);
    }

    /**
     * 图书数量加一
     *
     * @param bookId 图书编号
     * @return int
     */
    public int updateByIdAddNumber(Integer bookId) {

        //编写sql语句
        String sql = "UPDATE book_info\n" +
                "SET number = CASE \n" +
                "                 WHEN number IS NOT NULL THEN number + 1 \n" +
                "                 ELSE number \n" +
                "               END\n" +
                "WHERE book_id = ?";
        log.info("sql语句：{}", sql);
        //执行sql语句
        return BaseDao.executeUpdate(sql, bookId);
    }
}
